package com.jic.point.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.MoniInterface;
import com.jic.point.model.entity.MoniInterfaceApply;
import com.jic.point.model.vo.request.MoniInterfaceApplyRequest;
import com.jic.point.model.vo.response.MoniInterfaceApplyResponse;
import com.jic.point.service.MoniInterfaceApplyService;
import com.jic.point.service.MoniInterfaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: admin
 * @date: 2020-11-02 21:19:42
 */
 @RestController
 @Slf4j
 @RequestMapping("/moniInterfaceApply")
 @Api(tags = " [接口监控表]控制类-PC")
public class MoniInterfaceApplyController {
	@Autowired
	private MoniInterfaceApplyService service;
    @Autowired
    private MoniInterfaceService moniInterfaceService;
	
     /**
      *新增记录
      * @param request 新增[接口监控表]请求参数
      * @return MoniInterfaceApplyResponse 新增[接口监控表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[接口监控表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MoniInterfaceApplyRequest request) throws Throwable {
     		MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
    		BeanUtils.copyProperties(request, moniInterfaceApply);
    	    long flag = service.save(moniInterfaceApply);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[接口监控表]请求参数
      * @return MoniInterfaceApplyResponse 新增或修改[接口监控表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[接口监控表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody MoniInterfaceApplyRequest request) throws Throwable {
      		MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
		     BeanUtils.copyProperties(request, moniInterfaceApply);
	         long flag =   service.saveOrUpdate(moniInterfaceApply);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[接口监控表]请求参数
      * @return MoniInterfaceApplyResponse 删除[接口监控表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[接口监控表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody MoniInterfaceApplyRequest request) throws Throwable {
      		MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
	     	BeanUtils.copyProperties(request, moniInterfaceApply);
	         long flag = service.deleteByPrimaryKeyLogically(moniInterfaceApply);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[接口监控表]请求参数
      * @return MoniInterfaceApplyResponse 修改[接口监控表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[接口监控表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody MoniInterfaceApplyRequest request) throws Throwable {
      		MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
	     	BeanUtils.copyProperties(request, moniInterfaceApply);
	         long flag = service.updateByPrimaryKeySelective(moniInterfaceApply);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[接口监控表]详情请求参数
      * @return MoniInterfaceApplyResponse 查询[接口监控表]详情响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[接口监控表]详情")
      @ResponseBody
     public RestResult<MoniInterfaceApplyResponse> getByPrimaryKey(@Validated  @RequestBody MoniInterfaceApplyRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
	     	BeanUtils.copyProperties(request, moniInterfaceApply);
	     	MoniInterfaceApply result = service.getByPrimaryKey(moniInterfaceApply);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     MoniInterfaceApplyResponse bo = new MoniInterfaceApplyResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new MoniInterfaceApplyResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<MoniInterfaceApply>> listPage (@RequestBody MoniInterfaceApplyRequest request) throws Throwable {
      MoniInterfaceApply param = new MoniInterfaceApply();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *MONI_INTERFACE_APPLY 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [接口监控表] ")
      @ResponseBody
      public RestResult<List<MoniInterfaceApplyResponse>>  listMoniInterfaceApplyAllRecord(@RequestBody MoniInterfaceApplyRequest request) throws Throwable {
      		MoniInterfaceApply param = new MoniInterfaceApply();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<MoniInterfaceApply> list = service.listAllRecord(param);
			List<MoniInterfaceApplyResponse> responseList = list.stream().map(temp -> {
			MoniInterfaceApplyResponse bo = new MoniInterfaceApplyResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

    /**
     * 审核提交
     */
    @PostMapping("/auditAubmit")
    @ApiOperation(value = "审核提交 [第三方机构主表] ")
    @ResponseBody
    public RestResult<Long> auditAubmit(@Validated @RequestBody MoniInterfaceApplyRequest request) throws Throwable{
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        //根据id查询信息
        MoniInterfaceApply moniInterfaceApply = new MoniInterfaceApply();
        BeanUtils.copyProperties(request, moniInterfaceApply);
        MoniInterfaceApply result = service.getByPrimaryKey(moniInterfaceApply);
        //赋值
        result.setAuditSts(request.getAuditSts());
        result.setAuditorReason(request.getAuditorReason());
        result.setAuditorId(request.getAdminOperate().getOperator());
        //申请表逻辑删除1
        if(request.getAuditSts()==2){
         service.deleteByPrimaryKeyLogically(moniInterfaceApply);
        }
        if(request.getAuditSts()==1){
            MoniInterface moniInterface=new MoniInterface();
            BeanUtils.copyProperties(result, moniInterface);
            moniInterface.setId(null);
            int i = moniInterfaceService.saveOrUpdate(moniInterface);
            service.deleteByPrimaryKey(result);
        }
        if(request.getAuditSts()==3){
            service.saveOrUpdate(result);
        }

        return RestResult.success(1L);

    }

     }

	
